package com.kld.simplelauncher.app;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kld.simplelauncher.R;
import com.kld.simplelauncher.utils.EntryActivity;

import java.util.List;

public class AllAppAdapter extends RecyclerView.Adapter<AllAppAdapter.ViewHolder> {
    private static final String TAG = "AllAppAdapter";
    private List<EntryActivity> appList;
    private Context mContext;
    private OnAppClickListener listener;

    public AllAppAdapter(Context context,List<EntryActivity> apps) {
        this.appList = apps;
        this.mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 创建每页视图
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.app_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        EntryActivity info = appList.get(position);
        if (info != null) {
//            Drawable appIcon = pm.getApplicationIcon(info.applicationInfo);
            holder.appIcon.setImageDrawable(info.getIcon());
            holder.appName.setText(info.displayName);
            holder.itemView.setTag(info);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onAppClickListener(holder.itemView,position,info);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                if (listener != null) {
                    listener.onAppLongClickListener(holder.itemView,position,info);
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        if (appList != null && !appList.isEmpty()) return appList.size();
        return 0;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setAppList(List<EntryActivity> appList) {
        this.appList = appList;
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView appName;
        private final ImageView appIcon;

        public ViewHolder(View itemView) {
            super(itemView);
            appName = itemView.findViewById(R.id.app_name);
            appIcon = itemView.findViewById(R.id.app_icon);
        }
    }

    public void addListener(OnAppClickListener listener) {
        this.listener = listener;
    }

    public interface OnAppClickListener{
        void onAppClickListener(View view,int position,EntryActivity info);
        void onAppLongClickListener(View view,int position,EntryActivity info);
    }
}
